<script>
	import utils from "@/common/utils";
	import conf from "@/common/conf.js";
	import vers from "@/common/version.js";
	import psMethod from "@/common/psMethod";
	export default {
		globalData: {
			run: "pro",
			ver: "1.0.0",
			state: true, // 后端通讯状态
			stamp: null,
			host: null,
			auth: null,
			token: null,
			iH: 0,
			logo: "/static/logo2.png",
			title: "",
			city: null,
			scene: null, // 场景值
			isReady: false, // 是否加载就绪
		},
		onLaunch: async function(options) {
			console.log("App onLaunch ==============================");
			this.scene = options.scene;

			// 1__加载运行环境
			this.globalData.pics = conf.pics;
			let run = uni.getAccountInfoSync();
			console.log("1_加载运行环境", run.miniProgram);
			if (run.miniProgram.envVersion === "trial") {
				console.log("运行环境__体验版");
				this.globalData.host = 'https://dev.yuejunxi.com';
				this.globalData.run = "trial";
				this.globalData.ver = "体验版 " + vers.version;
				this.globalData.stamp = vers.stamp;
			} else if (run.miniProgram.envVersion === "release") {
				console.log("运行环境__正式版");
				this.globalData.host = 'https://api.yuejunxi.com';
				this.globalData.run = "release";
				this.globalData.ver = run.miniProgram.version;
				this.globalData.stamp = vers.stamp;
			} else {
				console.log("运行环境__未知");
				this.globalData.host = conf.tal_host;
				this.globalData.ver = vers.version;
				this.globalData.stamp = vers.stamp;
			}

			if (run.miniProgram.envVersion === "develop") {
				console.log("运行环境__开发版");
				this.globalData.host = conf.dev_host;
				this.globalData.run = "develop";
				this.globalData.ver = "开发版 " + vers.version;
				this.globalData.stamp = vers.stamp;
			}

			// 加载窗口高度
			const wHand = uni.getWindowInfo();
			this.globalData.iW = wHand.windowWidth;
			this.globalData.iH = wHand.windowHeight;
			this.globalData.tH =
				wHand.safeAreaInsets.top + wHand.safeAreaInsets.bottom + 10;
			if (this.globalData.tH < 55) {
				this.globalData.tH = 55;
			}
			console.log("2_加载全局参数", wHand);

			try {
				let prefix = this.globalData.run === "trial" ? "dev" : "";
				let stamp = Date.now();
				let token = uni.getStorageSync(prefix + "token");
				let auth = uni.getStorageSync(prefix + "auth");
				let last = uni.getStorageSync(prefix + "last");
				if (token && auth && stamp - last < 10 * 3600 * 1000) {
					console.log("5__检测登录", true);
					this.globalData.token = token;
					this.globalData.auth = auth;
				} else {
					console.log("5__检测登录", false);
					uni.clearStorageSync();
				}
			} catch (e) {
				//TODO handle the exception
				console.log("加载本地缓存失败", e);
			}

			if (!this.globalData.auth) {
				console.log('6__未登录');
				// this.autoLogin()
				let code = await uni.login({
					provider: 'weixin',
					onlyAuthorize: true
				})
				console.log('wxCode', code.code);
				let login = await this.hasWxCode(code.code).then(res => {
					console.log('自动登录完成->', res);
					this.globalData.auth = res.auth
					this.globalData.token = res.token
				})
				console.log('logined');
			}

			console.log("App onLaunch ed");
		},
		onReady() {
			console.log("App onReady =====================");
		},
		onShow: function() {
			console.log("App onShow =====================");
		},
		onHide: function() {
			console.log("App Hide =====================");
		},
		methods: {
			gotoNav() {
				uni.navigateTo({
					url: '/pages/index/index'
				})
			},
			async autoLogin() {
				let code = await uni.login({
					provider: 'weixin',
					onlyAuthorize: true
				})
				let login = await this.hasWxCode(code.code).then(res => {
					console.log('自动登录完成->', res);
					this.globalData.auth = res.auth
					this.globalData.token = res.token
				})

			},
			hasWxCode(e) {
				let host = this.globalData.host + '/weapps/login/'
				let data = {
					code: e
				}
				return new Promise((resolve, reject) => {
					psMethod.requestPostSync(host, data).then(res => {
						console.log('微信快速登录->', res);
						resolve(res.resmsg)
					}).catch(err => {
						reject(err)
					})
				})
			},
			/**
			 * 检测是否为数字
			 */
			isNumber(e) {
				return !isNaN(parseInt(e)) && isFinite(e);
			},
			async loadDefData(options) {
				console.log("loadDefData onLaunched...", options.scene);
				this.loadDataTitle();
				if (this.globalData.run === "trial") {
					console.log("4___加载城市位置");
					this.loadDataCity();
				}
				console.log("...环境加载完成...");

				try {
					let prefix = this.globalData.run === "trial" ? "dev" : "";
					let stamp = Date.now();
					let token = uni.getStorageSync(prefix + "token");
					let auth = uni.getStorageSync(prefix + "auth");
					let last = uni.getStorageSync(prefix + "last");
					if (token && auth && stamp - last < 10 * 3600 * 1000) {
						console.log("5__检测登录", true);
						this.globalData.token = token;
						this.globalData.auth = auth;
					} else {
						console.log("5__检测登录", false);
						uni.clearStorageSync();
					}
				} catch (e) {
					//TODO handle the exception
					console.log("加载本地缓存失败", e);
				}

				if (!Boolean(this.globalData.auth) || !Boolean(this.globalData.token)) {
					console.log("6__用户登录...");
					await this.fastLogin();
				}

				let pt = null;
				if (Boolean(options.query.scene)) {
					pt = decodeURIComponent(options.query.scene).split("=")[1];
				} else {
					pt = options.query.parent || options.query.staff || null;
				}

				console.log("推荐者信息", pt);

				if (
					!Boolean(this.globalData.auth?.parent) &&
					Boolean(pt) &&
					this.globalData.auth?.id !== pt
				) {
					console.log("7__绑定推荐", pt);
					await this.bindParentStaff(pt);
				}

				if (
					options.scene === 1007 ||
					options.scene === 1008 ||
					options.scene === 1155
				) {
					// if (options.scene === 1155) {
					// 	// 1154 为预览页面
					// 	console.log('来自于_微信朋友圈', options.path, options.query);
					// } else {
					// 	console.log('来自于_微信分享', options.path, options.query);
					// }
					// 1049 微信扫码
				}

				this.globalData.isReady = true;
				console.log("__init__");
			},
			loadDataEnv() {
				return new Promise((resolve, reject) => {
					this.globalData.pics = conf.pics;
					// 加载运行环境
					let run = uni.getAccountInfoSync();
					console.log("1_加载运行环境", run.miniProgram);
					if (run.miniProgram.envVersion === "develop") {
						this.globalData.host = conf.dev_host;
						this.globalData.run = "develop";
						this.globalData.ver = "开发版 " + vers.version;
						this.globalData.stamp = vers.stamp;
					} else if (run.miniProgram.envVersion === "trial") {
						this.globalData.host = conf.tal_host;
						this.globalData.run = "trial";
						this.globalData.ver = "体验版 " + vers.version;
						this.globalData.stamp = vers.stamp;
					} else if (run.miniProgram.envVersion === "release") {
						this.globalData.host = conf.host;
						this.globalData.run = "release";
						this.globalData.ver = "正式版 " + run.miniProgram.version;
						this.globalData.stamp = vers.stamp;
					} else {
						this.globalData.host = conf.host;
						this.globalData.ver = vers.version;
						this.globalData.stamp = vers.stamp;
					}
					resolve("success");
				});
			},
			/**
			 * 加载全局参数
			 */
			loadDataKwargs() {
				return new Promise((resolve, reject) => {
					// 加载窗口高度
					const wHand = uni.getWindowInfo();
					// const aDeve = uni.getDeviceInfo();
					// const aBase = uni.getAppBaseInfo();
					console.log("2_加载全局参数", wHand);
					this.globalData.iW = wHand.windowWidth;
					this.globalData.iH = wHand.windowHeight;
					this.globalData.tH =
						wHand.safeAreaInsets.top + wHand.safeAreaInsets.bottom + 10;

					if (this.globalData.tH < 55) {
						this.globalData.tH = 55;
					}

					resolve("success");
				});
			},
			/**
			 * 加载全局标题
			 */
			loadDataTitle() {
				let that = this;
				// 加载后端数据
				let host = that.globalData.host + "/api/conf/";
				let data = {
					key: "system_title",
				};
				utils
					.httpRequestOnList(host, data)
					.then((res) => {
						console.log("3_异步加载全局配置_标题", res);
						that.globalData.title = res.find(
							(item) => item.key === "system_title"
						).value;
					})
					.catch((err) => {
						that.globalData.state = false;
					});
			},
			/**
			 * 加载城市位置
			 */
			loadDataCity() {
				let that = this;
				uni.getStorage({
					key: "city",
					success(res) {
						console.log("4_异步加载城市", res);
						let cur = Date.parse(new Date());
						if (res.data.stamp + 8 * 60 * 60 > cur) {
							uni.removeStorage({
								key: "city",
							});
						} else {
							that.globalData.city = res.data.city;
						}
					},
				});
			},
			/**
			 * 快速登录
			 */
			fastLogin() {
				console.log("启动快速登录");
				let that = this;
				let prefix = this.globalData.run === "trial" ? "dev" : "";

				return new Promise((resolve, reject) => {
					uni.login({
						provider: "weixin",
						onlyAuthorize: true,
						success(code) {
							// console.log('微信code', code);
							let host = that.globalData.host + "/weapps/login/";
							let data = {
								code: code.code,
							};
							utils
								.httpRequestOnPost(host, data)
								.then((res) => {
									console.log("6__用户登录成功", res);
									that.globalData.token = res.token;
									that.globalData.auth = res.auth;
									resolve("success");
								})
								.catch((err) => {
									console.log("6__用户登录失败", err);
									reject(err);
								});
						},
						fail(err) {
							reject(err);
						},
					});
				});
			},
			/**
			 * 绑定推荐
			 */
			bindParentStaff(e) {
				console.log("bindParentStaff", e);
				let that = this;
				let prefix = this.globalData.run === "trial" ? "dev" : "";
				let host = this.globalData.host + "/weapps/bindHelp/";
				let token = this.globalData.token || "";
				let data = {
					parent: e,
				};
				return new Promise((resolve, reject) => {
					utils.authenticatorOnPost(host, token, data).then((res) => {
						console.log("6__绑定推荐", res);
						that.globalData.auth = res;
						uni.setStorageSync(prefix + "auth", res);
					});
				});
			},
		},
	};
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/tuniaoui-vue3/index.css";
	@import "@/uni_modules/vin-ui/style.css";
	@import "uni.scss";
	@import "index.css";

	@font-face {
		font-family: "AiDianFengYaHe";
		src: url("https://cos.yuejunxi.com/media/fonts/AiDianFengYaHe-2.ttf") format("truetype");
		font-weight: normal;
		font-style: normal;
	}

	.top-font-title {
		font-family: "AiDianFengYaHe", sans-serif;
	}

	.template-home {
		max-height: 100vh;
	}

	.container {
		max-height: 100vh;
		padding: 15px;
		background-color: #f1f1f1;
		height: 100vh;
	}

	.theme-container {
		background: linear-gradient(to bottom, $uni-color-banner, #f1f1f1);
	}

	.tn-color-white {
		color: #fff;
	}

	.cqbb-font {
		/* font-family: chongqingbangbang; */
		font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	}

	/* 公共按钮样式 */
	.actButton {
		padding: 20px 40px;
		border-radius: 5px;
		display: flex;
		justify-content: center;
		align-items: center;
		gap: 10px;
	}

	.bg-wxpay {
		background-color: #46ad0f;
		color: #fff;
	}

	/* 卡片网格样式 */
	.card-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-template-rows: repeat(6, 1fr);
		gap: 10px;
	}

	/* 卡片网格样式 */
	.card {
		background: white;
		border-radius: 10px;
		padding: 10px 20px;
		color: white;
		position: relative;
		overflow: hidden;
		box-shadow: 0 2px 20px 1px rgba(0, 0, 0, 0.3);
	}

	.card::after {
		content: "";
		position: absolute;
		right: -30px;
		bottom: -30px;
		width: 100px;
		height: 100px;
		border-radius: 50%;
		background: rgba(255, 255, 255, 0.1);
	}

	/* 卡片颜色变体 */
	.purple {
		background: linear-gradient(135deg, #9c27b0, #673ab7);
	}

	.gray {
		background: linear-gradient(135deg, #607d8b, #455a64);
	}

	.sunset {
		background: linear-gradient(135deg, #ff6b6b, #ffa07a);
	}

	.ocean {
		background: linear-gradient(135deg, #00f2fe, #4facfe);
	}

	.forest {
		background: linear-gradient(135deg, #43a047, #1de9b6);
	}

	.berry {
		background: linear-gradient(135deg, #ff4081, #7b1fa2);
	}

	.midnight {
		background: linear-gradient(135deg, #2c3e50, #3498db);
	}

	.autumn {
		background: linear-gradient(135deg, #ff9a44, #fc6076);
	}

	.mint {
		background: linear-gradient(135deg, #00b09b, #96c93d);
	}

	.lavender {
		background: linear-gradient(135deg, #a18cd1, #fbc2eb);
	}

	.coral {
		background: linear-gradient(135deg, #ff7e5f, #feb47b);
	}

	/* start 全屏适配 */
	.window-top {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	.window-flex {
		flex: 1;
		overflow: hidden;
	}

	.window-box {
		height: 100%;
		overflow: hidden;
	}

	.window-scroll {
		height: 100%;
		overflow: hidden;
	}

	/* end 全屏适配 */

	/* 头部栏 */
	.malltHeadBatten {
		background: linear-gradient(to bottom, $uni-color-banner, #f1f1f1);
	}

	/* 文本容器 */
	.textView {
		max-width: 150px;
		/* 限制最大宽度 */
		white-space: nowrap;
		/* 防止文本换行 */
		overflow: hidden;
		/* 隐藏溢出的内容 */
		text-overflow: ellipsis;
		/* 显示省略符号来代表被修剪的文本 */
	}

	.textViewShort {
		max-width: 80px;
		/* 限制最大宽度 */
		white-space: nowrap;
		/* 防止文本换行 */
		overflow: hidden;
		/* 隐藏溢出的内容 */
		text-overflow: ellipsis;
		/* 显示省略符号来代表被修剪的文本 */
	}

	/* 默认按钮类 */
	.gl-button-base {
		background-color: #b58d63;
		border-color: #70552d;
		color: #fff;
	}

	.gl-button-base-hover {
		background-color: #f3eacd;
		border-color: #fff5d7;
		color: #70552d;
	}

	.gl-base-input {
		color: #222;
		background-color: #f1f1f1;
	}

	/* popup弹窗 */
	.gl-pop-page {
		padding: 15px;
	}
</style>